from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from subjects import models
from subjects import subser
# Create your views here.
#添加一级分类
class CateView(APIView):
    def post(self, request):
        ser=subser.CateSerializer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({"msg":"添加成功","data":ser.data})
#展示一级分类
class CateShowView(APIView):
    def get(self, request):
        catelist=models.CateModels.objects.all()
        ser=subser.CateSerializer(catelist,many=True)
        return Response({"msg":"ok","data":ser.data})
#添加二级分类
class CateUpdateView(APIView):
    def post(self, request):
        ser=subser.CateUpdateSerializer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({"msg":"添加成功","data":ser.data})
#展示二级分类
class CateUpdateShowView(APIView):
    def get(self, request):
        catelist=models.Cateupdate.objects.all()
        ser=subser.CateUpdateSerializer(catelist,many=True)
        return Response({"msg":"ok","data":ser.data})

#一级分类二级分类

class CateAllView(APIView):
    def get(self, request):
        sid=request.query_params.get("sid")
        catelist=models.Cateupdate.objects.filter(cate=sid)
        ser=subser.CateUpdateSerializer(catelist,many=True)
        return Response({"msg":"ok","data":ser.data})

#另一个列表

#添加一级分类
class Catetoow(APIView):
    def post(self, request):
        ser=subser.CateSer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({"msg":"添加成功","data":ser.data})
#展示一级分类
class CateShowtoow(APIView):
    def get(self, request):
        catelist=models.CateModels2.objects.all()
        ser=subser.CateSer(catelist,many=True)
        return Response({"msg":"ok","data":ser.data})
#添加二级分类
class CateUpdatetoow(APIView):
    def post(self, request):
        ser=subser.CateUpdateSer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({"msg":"添加成功","data":ser.data})
#展示二级分类
class CateUpdateShowtoow(APIView):
    def get(self, request):
        catelist=models.Cateupdate2.objects.all()
        ser=subser.CateUpdateSer(catelist,many=True)
        return Response({"msg":"ok","data":ser.data})

#一级分类二级分类

class Catealltoow(APIView):
    def get(self, request):
        sid=request.query_params.get("sid")
        catelist=models.Cateupdate2.objects.filter(cate=sid)
        ser=subser.CateUpdateSer(catelist,many=True)
        return Response({"msg":"ok","data":ser.data})



